package core;

import java.awt.*;
import java.io.*;

import ui.ScreenDisplay;

public class Preferences implements Serializable {
	
//	private static final long serialVersionUID = -8654184709331704072L;
	
	public static final File BASE_FOLDER = new File("data/"),
								DEFAULT_FILE = new File("data/prefs.dat"),
								SONG_INDEX_FILE = new File("data/songs.dat"),
								SONG_DIR = new File("data/songs/"),
								SET_INDEX_FILE = new File("data/sets.dat");
	
	private Font mainFont = new Font("Dialog", Font.PLAIN, 44),
				cjkFont = new Font("Dialog", Font.PLAIN, 72);
	private int defaultMonitor = 0;
	private int align = ScreenDisplay.CENTER;
	private Color bgColor = Color.black,
				fgColor = Color.white;
	
	public boolean debugMode = false;
	public File lastImageLocation = null;
	
	// find the screen devices and their configurations
	public static final GraphicsConfiguration[] gcs;
	static {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice[] gds = ge.getScreenDevices();
		gcs = new GraphicsConfiguration[gds.length];
		for (int i=0; i<gds.length; i++)
			gcs[i] = gds[i].getDefaultConfiguration();
	}
	
	
	public int getDefaultMonitor() {
		if (defaultMonitor >= gcs.length)
			defaultMonitor = 0;
		
		return defaultMonitor;
	}
	public void setDefaultMonitor(int newMon) {
		defaultMonitor = newMon;
	}
	public GraphicsConfiguration getDefaultMonitorConfig() {
		return gcs[getDefaultMonitor()];
	}
	
	public Color getBgColor() {
		return bgColor;
	}
	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
	}
	public Color getFgColor() {
		return fgColor;
	}
	public void setFgColor(Color fgColor) {
		this.fgColor = fgColor;
	}
	
	public int getAlign() {
		return align;
	}
	public void setAlign(int align) {
		this.align = align;
	}
	
	
	public Font getMainFont() {
		return mainFont;
	}
	public void setMainFont(Font mainFont) {
		this.mainFont = mainFont;
	}
	public Font getCJKFont() {
		return cjkFont;
	}
	public void setCJKFont(Font cjkFont) {
		this.cjkFont = cjkFont;
	}
	
	
	public void save() {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(DEFAULT_FILE));
			oos.writeObject(this);
			oos.close();
			
		} catch(IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public static Preferences load(File f) {
		if(!f.exists())
			return null;
		
		Preferences p = null;
		try {
			ObjectInputStream ois = new ObjectInputStream(
					new FileInputStream(f));
			p = (Preferences)ois.readObject();
			ois.close();
			
		} catch(InvalidClassException ex) {
			System.err.println("Outdated Preferences file; using default preferences");
		} catch(IOException ex) {
			ex.printStackTrace();
		} catch(ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		
		return p;
	}
}
